<?php
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}

    $objXajax->registerFunction('frmchartincomebymonth_viewchart'); 
   
    
    function frmchartincomebymonth_viewchart($charttype=1,$frommonth='',$fromyear='',$tomonth='',$toyear=''){
    
        global $objDbSelect;
         $arrTitle = array(
            1=>'Tổng hợp doanh thu',
            2=>'Thu tiền phòng',
            3=>'Thu tiền sản phẩm',
            4=>'Tiền phòng và sản phẩm',
            5=>'Tiền chi'
         );
        $objResponse = new xajaxResponse();
        include(ROOT_PATH.'adagps.com/Charts/ofc/php-ofc-library/open-flash-chart.php');    
         $frommonth =$frommonth<10?'0'.$frommonth:$frommonth;
         $tomonth =$tomonth<10?'0'.$tomonth:$tomonth;
        $arrResult = frmchartincomebymonth_createArray($charttype,"$fromyear-$frommonth","$toyear-$tomonth");
        
        $arrName = Array(); 
        $max = 5;
        $title1 = new title( $arrTitle[1],20) ;
        $title2 = new title( $arrTitle[2],20) ;
        $title3 = new title( $arrTitle[3],20) ;
        $title4 = new title( $arrTitle[5],20) ;
        $format = 2;
        $sName='';
        $barroomprice = array();
        $bartotalprice = array();
        $barpayment = array();
        $barproductprice = array();
        $barArr2 = array();
        $barLabel = array();
        $maxpay =0;
        $maxget =0;
        $chart1 = new open_flash_chart();
        $chart2 = new open_flash_chart();
        $chart3 = new open_flash_chart();
        $chart4 = new open_flash_chart();

        for($i=0;$i<count($arrResult);$i++){
            $sName =  $arrResult[$i]['dateend'];
            $xx = new x_axis_label($sName, '#050069', 13, 340);            
            $barLabel[] = $xx;
            $barroomprice[]=(int)$arrResult[$i]['roomprice'];
            $bartotalprice[]=(int)$arrResult[$i]['totalprice'];
            $barpayment[]=(int)$arrResult[$i]['payment'];
            $barproductprice[]=(int)$arrResult[$i]['productprice'];
            if($maxget<max($arrResult[$i]['totalprice'],$arrResult[$i]['roomprice'],$arrResult[$i]['productprice'])){
                $maxget=max($arrResult[$i]['totalprice'],$arrResult[$i]['roomprice'],$arrResult[$i]['productprice']);
            }
          }
   
        $payment = new bar_glass();
        $roomprice = new bar_glass();
        $productprice = new bar_glass();
        $totalprice = new bar_glass();
        $payment->set_colour('#68D5ED');
        $payment->set_values($barpayment);
        $payment->key('Chi',12);
        
        
        $roomprice->set_colour('#ff00ff');
        $roomprice->set_values($barroomprice);
        $roomprice->key('Tiền phòng',12);
        
        
        $productprice->set_colour('#4FA0D6');
        $productprice->set_values($barproductprice);
        $productprice->key('Tiền sản phẩm',12);
        
        
        $totalprice->set_colour('#C64FD6');
        $totalprice->set_values($bartotalprice);
        $totalprice->key('Tổng thu nhập',12);
            
           


        $rangeMax =0;
        $rangeMin =0;
        $rangStep=1;
        createRangeForChart(-$maxpay,$maxget,$rangeMax,$rangeMin);
        $y = new y_axis();
        $y->set_range($rangeMin,$rangeMax);
        $y->set_steps(($rangeMax-$rangeMin)/5);
        
        $x_labels = new x_axis_labels();
        $x = new x_axis();
        $x_labels->set_labels($barLabel );
        $x->set_labels($x_labels);
        
        $chart1->add_element($totalprice);
        $chart2->add_element($roomprice);
        $chart3->add_element($productprice);
        $chart4->add_element($payment);
            

        //if($charttype==1){
//            $chart->add_element($payment);
//            $chart->add_element($roomprice);
//            $chart->add_element($productprice);
//            $chart->add_element($totalprice);
//             
//        }else if($charttype==2){
//            $chart->add_element($roomprice); 
//        }else if($charttype==3){
//             $chart->add_element($productprice);
//        }else if($charttype==4){
//             $chart->add_element($roomprice);
//            $chart->add_element($productprice);
//            $chart->add_element($totalprice);
//        }else if($charttype==5){
//             $chart->add_element($payment);
//        }
        $chart1->set_x_axis($x);
        $chart1->set_y_axis($y);
        $chart1->set_title($title1);
        
        $chart2->set_x_axis($x);
        $chart2->set_y_axis($y);
        $chart2->set_title($title2);
        
        $chart3->set_x_axis($x);
        $chart3->set_y_axis($y);
        $chart3->set_title($title3);
        
        $chart4->set_x_axis($x);
        $chart4->set_y_axis($y);
        $chart4->set_title($title4);
        
        $values1 = $chart1->toPrettyString();
        $values2 = $chart2->toPrettyString();
        $values3 = $chart3->toPrettyString();
        $values4 = $chart4->toPrettyString();
        
       $sScript1 = 'data_chartincomebymonth1=' . $values1 .';';                
       $sScript2 = 'data_chartincomebymonth2=' . $values2 .';';                
       $sScript3 = 'data_chartincomebymonth3=' . $values3 .';';                
       $sScript4 = 'data_chartincomebymonth4=' . $values4 .';';                
        
       $objResponse->addScript($sScript1);    
       $objResponse->addScript($sScript2);    
       $objResponse->addScript($sScript3);    
       $objResponse->addScript($sScript4);    
        $sScript1 = 'swfobject.embedSWF("Charts/ofc/open-flash-chart.swf", "frmchartincomebymonth_divGrid1", "90%", "280", "9.0.0", "expressInstall.swf", {"get-data":"open_flash_chart_data_bymonth1"}, {"wmode":"opaque"});';
        $sScript2 = 'swfobject.embedSWF("Charts/ofc/open-flash-chart.swf", "frmchartincomebymonth_divGrid2", "90%", "280", "9.0.0", "expressInstall.swf", {"get-data":"open_flash_chart_data_bymonth2"}, {"wmode":"opaque"});';
        $sScript3 = 'swfobject.embedSWF("Charts/ofc/open-flash-chart.swf", "frmchartincomebymonth_divGrid3", "90%", "280", "9.0.0", "expressInstall.swf", {"get-data":"open_flash_chart_data_bymonth3"}, {"wmode":"opaque"});';
        $sScript4 = 'swfobject.embedSWF("Charts/ofc/open-flash-chart.swf", "frmchartincomebymonth_divGrid4", "90%", "280", "9.0.0", "expressInstall.swf", {"get-data":"open_flash_chart_data_bymonth4"}, {"wmode":"opaque"});';
        $objResponse->addScript($sScript1);    
       $objResponse->addScript($sScript2);    
       $objResponse->addScript($sScript3);    
       $objResponse->addScript($sScript4);   
        $objResponse->addScript("changeWaiting(1);");  
                          
        return $objResponse->getXML();    
    }
    
function frmchartincomebymonth_createArray($charttype,$dateFrom,$dateTo){
    global $objDbUpdate,$tbl_postfix;
    
    $sql= "select sum(roomprice) as roomprice,dateend,sum(totalprice) as totalprice,sum(pay) as payment from  (select price as roomprice , DATE_FORMAT(timeend, '%Y-%m') as dateend,totalprice as totalprice,0 as pay
     from tbl_history_book_room$tbl_postfix  where  DATE_FORMAT(timeend, '%Y-%m') >= '$dateFrom' and DATE_FORMAT(timeend, '%Y-%m') <= '$dateTo' UNION ALL ";
     $sql.=" select 0 as roomprice , DATE_FORMAT(date_created, '%Y-%m') as dateend,0 as totalprice, money
     from tbl_pay$tbl_postfix where  DATE_FORMAT(date_created, '%Y-%m') >= '$dateFrom' and DATE_FORMAT(date_created, '%Y-%m') <= '$dateTo') as tbl_temp   group by dateend order by dateend";
    
 $sumroomprice = 0;
 $sumtotalprice = 0;
    $arr = $objDbUpdate->GetArray($sql);
    for($i=0;$i<count($arr);$i++){
      $arr[$i]['productprice'] = $arr[$i]['totalprice'] - $arr[$i]['roomprice'];
      $arr[$i]['totalprice']  =  $arr[$i]['totalprice']- $arr[$i]['payment'];
    }    
    //echo '<pre>'; print_r($arr);die();
    return  $arr  ;
}


   
?>